#coding:utf-8
"""
服务器信息操作
"""
import public
import database

class servicer():
    def __init__(self):
        self.db=database.database()

    def get_infos(self,id=None):
        """
        如果没有定义id，则返回所有结果
        """
        where=''
        if id:
            where='where servicer.id = %s'%(id)
        cursor=self.db.execute(r'''select servicer.id,servicer.loginstr,place.place,system.system,project.project,sa.sa,servicer.ip,servicer.comment,type.type from servicer
          inner join place on servicer.place = place.id
          inner join project on servicer.project = project.id
          inner join sa on servicer.sa = sa.id
          inner join system on servicer.system = system.id
          inner join type on servicer.type = type.id
          %s'''%(where))
        infos=[]
        rows=cursor.fetchall()
        for row in rows:
            d={}
            d['id']=row[0]
            d['loginstr']=row[1]
            d['place']=row[2]
            d['system']=row[3]
            d['project']=row[4]
            d['sa']=row[5]
            d['ip']=row[6].split(':')
            d['comment']=row[7]
            d['type']=row[8]

            if 'windows' in d['system'].lower():
                d['terminal']='windows'
                d['port']='3389'
            else:
                d['terminal']='linux'
                d['port']='22'
            infos.append(d)

        if id and len(infos)==1:
            return infos[0]#如果只获取一条信息,就直接返回字典
        return infos